<template>
  <div>
      <el-card>
 <div class="search_box">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            size="small"
            v-model="search.name"
            placeholder="请输入部门查询"
            @keyup.enter.native="handleSearch"
            clearable
            @clear="handleSearch"
          >
            <el-button
              slot="append"
              @click="handleSearch"
              icon="el-icon-search"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="16">
          <el-button type="success" icon="el-icon-plus" @click="handleAdd"   v-permission="'department:add'"
            >添加</el-button
          >
          <el-button icon="el-icon-download" @click="handleExport"
            >导出</el-button
          >
        </el-col>
      </el-row>
    </div>
    <div class="table_box">
      <el-table  height="460"  size="small" :data="depList" border stripe>
        <el-table-column width="80" align="center" prop="id" label="#">
        </el-table-column>
        <el-table-column align="center" prop="phone" label="办公电话">
        </el-table-column>
        <el-table-column align="center" prop="name" label="部门名称">
        </el-table-column>
        <el-table-column align="center" prop="total" label="人数" sortable>
          <template scope="scope">
            <el-tag>{{ scope.row.total }}人</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="createTime" label="创建时间" sortable>
        </el-table-column>
        <el-table-column align="center" prop="modifiedTime" label="修改时间" sortable>
        </el-table-column>
        <el-table-column align="center" prop="address" label="地址">
        </el-table-column>
        <el-table-column width="200" align="center" prop="id" label="操作">
          <template scope='scope'>
            <el-button icon="el-icon-edit" size="mini" type="primary" @click="handleEdit(scope.row)"
             v-permission="'department:edit'"
              >编辑</el-button
            >
            <el-button  v-permission="'department:delete'" icon="el-icon-delete" size="mini" type="danger" @click="delDep(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <page-tools
        :total="total"
        :paginationPage="search.pageNum"
        :paginationPagesize="search.pageSize"
        @pageSizeChange="pageSizeChange"
        @pageChange="pageChange"
      ></page-tools>
      <el-dialog
        @close="close"
        width="500px"
        :title="isEdit?'编辑部门':'新增部门'"
        :visible.sync="addShow"
      >
        <addDepartment v-if="addShow" :isEdit='isEdit' :obj='editRow' @close="close" @getDeps="getDepList"></addDepartment>
      </el-dialog>
    </div>
      </el-card>

  </div>
</template>

<script>
import addDepartment from './addDepartment.vue'
import { getDeptApi, exportDeptApi, delDeptApi } from '../../../api/department'
import pageTools from '../../../components/pageTools.vue'
export default {
  components: { pageTools, addDepartment },
  name: 'Departments',
  data () {
    return {
      depList: [],
      search: {
        name: '',
        pageSize: 5,
        pageNum: 1
      },
      total: 0,
      addShow: false,
      isEdit: false,
      editRow: {}
    }
  },
  methods: {
    handleEdit (row) {
      this.isEdit = true
      this.addShow = true
      this.editRow = row
    },
    // 删除
    delDep (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await delDeptApi(id)
        if (!res.success) return this.$notify.error(res.data.errorMsg)
        this.getDepList()
        this.$notify({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$notify({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    close () {
      this.addShow = false
    },
    handleAdd () {
      this.isEdit = false
      this.addShow = true
    },
    // 导出
    async handleExport () {
      const res = await exportDeptApi()
      const url = window.URL.createObjectURL(res)
      // 创建一个a链接
      const a = document.createElement('a')
      // 将a标签插入到body
      document.body.appendChild(a)
      a.href = url
      a.download = '部门列表.xls'
      a.click()
      window.URL.revokeObjectURL(url)
    },
    handleSearch () {
      this.search.pageNum = 1
      this.getDepList()
    },
    pageChange (val) {
      this.search.pageNum = val
      this.getDepList()
    },
    pageSizeChange (val) {
      this.search.pageSize = val
      this.getDepList()
    },
    async getDepList () {
      const res = await getDeptApi(this.search)
      if (!res.success) return this.$notify.error(res.data.errorMsg)
      this.depList = res.data.rows
      this.total = res.data.total
    }
  },
  created () {
    this.getDepList()
  }
}
</script>

<style lang='less' scoped>
.table_box {
  margin: 20px 0 0 0;
}
</style>
